--
-- Author: shunguo.chen
-- Date: 2017-12-18 16:34:36
--
-- MountGetView  坐骑获取界面

local fileName = string.getModeName(...)
local PartnerGetPop = class(fileName, app.mvc.ViewBase)

PartnerGetPop.VIEW_ROOT_RES = Res.PartnerGet

local SOUL_NUM_STR = L("lua_code_text_466")

function PartnerGetPop:init()
    display.setCascadeOpacityEnabled(self._root, true)
    self._ui.Button_ok:addClickEventListener(function()
        self:closeView()
    end)

    local con = self._ui["Panel/Panel_content"]
    self._tltText = con:getChildByName("tlt")

    self._listView = con:getChildByName("ListView")
    self._itemModel = self._listView:getItem(0)
    self._itemModel:retain()
    self._listView:removeAllItems()
    self:refresh()
end

function PartnerGetPop:remove()
    self._itemModel:release()
    self._itemModel = nil
end

function PartnerGetPop:refresh()
    if not self._data then
        return
    end
    self._ui.tlt:setString(self._data:getName())
    local resoruceStr = self._data:getResource()
    local resoruce = string.split(resoruceStr, ";")
   
    if #resoruce > 0 then
        for i,v in ipairs(resoruce) do
            local itemData = string.split(v, "|")
            local item = self._itemModel:clone()
            local featureId = tonumber( itemData[1] )
            local param = string.split(itemData[3], "#")
            self._listView:pushBackCustomItem(item)
            item:getChildByName("name"):setString(L(itemData[2]) or "")
            item:setPressedActionEnabled(false)
            item:addClickEventListener(function()
                self:hide(function(render)
                    display.resourceGotoFeature({feature = featureId, param = param})
                    executeFunc(self._onCloseView, self)
                end)
            end)
        end
    else
        self._listView:removeAllItems()
    end

    self._tltText:setString(L("ui_csd_15"))


    --显示灵魂石数量
    if self._data:isHave() then
        self._ui["Panel/Panel_content/Panel_bar"]:setVisible(false)
    else
        self._ui["Panel/Panel_content/Panel_bar"]:setVisible(true)
        local haveSoulNums = UD:getPartnerSoulCountById(self._data:getSoulStoneId())
        local needNums = self._data:getComposeNeedNum()
        if haveSoulNums >= needNums then
            self._ui["Panel/Panel_content/Panel_bar/LoadingBar"]:setPercent(100)
        else
            self._ui["Panel/Panel_content/Panel_bar/LoadingBar"]:setPercent(haveSoulNums/needNums * 100)
        end

        self._ui["Panel/Panel_content/Panel_bar/num"]:setString(string.format(L("lua_code_text_466"), haveSoulNums, needNums))
    end
end

return PartnerGetPop